From 97ac91e0bee7e3bb4601ff6e0edc76732ad1ce10 Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Tue, 4 Feb 2020 09:31:45 -0700 Subject: [PATCH] relocate no output format display routine. (#486) * relocate no output format display routine. waypt_disp() is used by main when no output format is specified. This moves that routine from waypt.cc to main.cc. * clean up exit from main. don't leave the global_waypoint_list lying around. return from main instead of calling exit. --- defs.h | 2 +- main.cc | 53 +++++++++++++++++++++++++++++++++++++++++++++++------ waypt.cc | 37 +++++++------------------------------ 3 files changed, 55 insertions(+), 37 deletions(-) diff --git a/defs.h b/defs.h index ea8a6413a..b1e456f8e 100644 --- a/defs.h +++ b/defs.h @@ -608,7 +608,6 @@ void waypt_init(); void waypt_add(Waypoint* wpt); void waypt_del(Waypoint* wpt); unsigned int waypt_count(); -void waypt_disp(const Waypoint* wpt); void waypt_status_disp(int total_ct, int myct); //void waypt_disp_all(waypt_cb); /* template */ //void waypt_disp_session(const session_t* se, waypt_cb cb); /* template */ @@ -618,6 +617,7 @@ void waypt_add_to_bounds(bounds* bounds, const Waypoint* waypointp); void waypt_compute_bounds(bounds* bounds); Waypoint* find_waypt_by_name(const QString& name); void waypt_flush_all(); +void waypt_deinit(); void waypt_append(WaypointList* src); void waypt_backup(WaypointList** head_bak); void waypt_restore(WaypointList* head_bak); diff --git a/main.cc b/main.cc index dba7917f0..bc7f56097 100644 --- a/main.cc +++ b/main.cc @@ -20,7 +20,6 @@ #include // for setlocale, LC_NUMERIC, LC_TIME #include // for signal, SIGINT, SIG_ERR #include // for printf, fgetc, stdin -#include // for exit #include // for strcmp #include // for QByteArray @@ -198,6 +197,43 @@ signal_handler(int sig) tracking_status.request_terminate = 1; } +class FallbackOutput +{ +public: + FallbackOutput() : mkshort_handle(mkshort_new_handle()) {} + // delete copy and move constructors and assignment operators. + // The defaults are not appropriate, and we haven't implemented proper ones. + FallbackOutput(const FallbackOutput&) = delete; + FallbackOutput& operator=(const FallbackOutput&) = delete; + FallbackOutput(FallbackOutput&&) = delete; + FallbackOutput& operator=(FallbackOutput&&) = delete; + ~FallbackOutput() {mkshort_del_handle(&mkshort_handle);} + + void waypt_disp(const Waypoint* wpt) + { + if (wpt->GetCreationTime().isValid()) { + printf("%s ", qPrintable(wpt->creation_time.toString())); + } + printposn(wpt->latitude,1); + printposn(wpt->longitude,0); + if (!wpt->description.isEmpty()) { + printf("%s/%s", + global_opts.synthesize_shortnames ? + qPrintable(mkshort(mkshort_handle, wpt->description)) : + qPrintable(wpt->shortname), + qPrintable(wpt->description)); + } + + if (wpt->altitude != unknown_alt) { + printf(" %f", wpt->altitude); + } + printf("\n"); + } + +private: + short_handle mkshort_handle; +}; + static int run(const char* prog_name) { @@ -210,6 +246,8 @@ run(const char* prog_name) int opt_version = 0; bool did_something = false; QStack qargs_stack; + FallbackOutput fbOutput; + // Use QCoreApplication::arguments() to process the command line. QStringList qargs = QCoreApplication::arguments(); @@ -518,7 +556,10 @@ run(const char* prog_name) } if (ovecs == nullptr) { cet_convert_init(CET_CHARSET_ASCII, 1); - waypt_disp_all(waypt_disp); + auto waypt_disp_lambda = [&fbOutput](const Waypoint* wpt)->void { + fbOutput.waypt_disp(wpt); + }; + waypt_disp_all(waypt_disp_lambda); } /* @@ -567,7 +608,7 @@ run(const char* prog_name) // ovecs->wr_position_deinit(); } else { /* Just print to screen */ - waypt_disp(wpt); + fbOutput.waypt_disp(wpt); } delete wpt; } @@ -665,12 +706,12 @@ main(int argc, char* argv[]) rc = run(prog_name); - waypt_flush_all(); route_deinit(); + waypt_deinit(); session_exit(); - Vecs::Instance().exit_vecs(); FilterVecs::Instance().exit_filter_vecs(); + Vecs::Instance().exit_vecs(); inifile_done(global_opts.inifile); - exit(rc); + return rc; } diff --git a/waypt.cc b/waypt.cc index 67921e8ac..db9375385 100644 --- a/waypt.cc +++ b/waypt.cc @@ -22,10 +22,8 @@ #include // for assert #include // for fabs #include // for printf, fflush, fprintf, stdout -#include // for time_t #include // for stable_sort -#include // for QByteArray #include // for QChar #include // for QDateTime #include // for QList @@ -42,7 +40,6 @@ WaypointList* global_waypoint_list; -static short_handle mkshort_handle; geocache_data Waypoint::empty_gc_data; static global_trait traits; @@ -54,7 +51,6 @@ const global_trait* get_traits() void waypt_init() { - mkshort_handle = mkshort_new_handle(); global_waypoint_list = new WaypointList; } @@ -92,29 +88,6 @@ waypt_count() return global_waypoint_list->count(); } -// TODO: should this, and mkshort_handle, be part of main, which is the only user? -void -waypt_disp(const Waypoint* wpt) -{ - if (wpt->GetCreationTime().isValid()) { - printf("%s ", qPrintable(wpt->creation_time.toString())); - } - printposn(wpt->latitude,1); - printposn(wpt->longitude,0); - if (!wpt->description.isEmpty()) { - printf("%s/%s", - global_opts.synthesize_shortnames ? - qPrintable(mkshort(mkshort_handle, wpt->description)) : - qPrintable(wpt->shortname), - qPrintable(wpt->description)); - } - - if (wpt->altitude != unknown_alt) { - printf(" %f", wpt->altitude); - } - printf("\n"); -} - void waypt_status_disp(int total_ct, int myct) { @@ -190,12 +163,16 @@ find_waypt_by_name(const QString& name) void waypt_flush_all() { - if (mkshort_handle) { - mkshort_del_handle(&mkshort_handle); - } global_waypoint_list->flush(); } +void +waypt_deinit() +{ + waypt_flush_all(); + delete global_waypoint_list; +} + void waypt_append(WaypointList* src) { -- 2.30.2